package com.financing.platform.web.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.NewsDTO;
import com.financing.platform.service.INewsService;
import com.financing.platform.service.IPublicActivitiesService;
import com.financing.platform.util.DTOdateSet;

@Controller("newsActionApi")
@RequestMapping("/api/news")
@Scope("prototype")
public class NewsAction extends BaseAction {

	@Autowired
	private INewsService newsServiceImpl;
	@Autowired
	private IPublicActivitiesService publicActivitiesServiceImpl;

	@RequestMapping("/queryMsgList.shtml")
	@ResponseBody
	public ResultMap queryMsgList(HttpServletRequest request, HttpServletResponse response, NewsDTO newsDTO, Pager page) {

		try {
			
			newsDTO.setBegin(page.getStartIndex());
			newsDTO.setPageSize(page.getPageSize());
			
			int count = newsServiceImpl.queryNewsListCount(newsDTO);
			page.setTotalCount(count);
			
			List<NewsDTO> listNews = newsServiceImpl.queryNewsList(newsDTO);

			resultMap.setAttribute("list", listNews);
			resultMap.setAttribute("pager", page);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query news list error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}

	@RequestMapping("/newsDetail.shtml")
	@ResponseBody
	public ResultMap newsDetail(HttpServletRequest request, HttpServletResponse response, NewsDTO newsDTO) {

		try {
			resultMap.setAttribute("dto", newsServiceImpl.queryNewsList(newsDTO).get(0));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query news list error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}

	@RequestMapping("/addMsg.shtml")
	@ResponseBody
	public ResultMap addMsg(HttpServletRequest request, HttpServletResponse response, NewsDTO newsDTO) {

		try {
			DTOdateSet.setCreateInfo(newsDTO, request.getSession());
			DTOdateSet.setUpdateInfo(newsDTO, request.getSession());
			
			resultMap.setAttribute("dto", newsServiceImpl.addNews(newsDTO));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query news list error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}

	@RequestMapping("/deleteMsg.shtml")
	@ResponseBody
	public ResultMap deleteMsg(HttpServletRequest request, HttpServletResponse response, String id) {

		try {
			if (id != null && !id.equals("")) {
				String[] ids = { id };
				newsServiceImpl.deleteNews(ids);
				resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			} else {
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
			}
		} catch (Exception e) {
			log.error("query news list error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}

	@RequestMapping("/updateMsg.shtml")
	@ResponseBody
	public ResultMap updateMsg(HttpServletRequest request, HttpServletResponse response, NewsDTO newsDTO) {

		try {
			DTOdateSet.setUpdateInfo(newsDTO, request.getSession());
			resultMap.setAttribute("dto", newsServiceImpl.updateNews(newsDTO));
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("query news list error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}

}
